package com.shanzmoo.db.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.db.product.entity.ProductSkuEntity;
import com.shanzmoo.db.product.entity.vo.SkuQueryVO;
import com.shanzmoo.db.product.entity.vo.SkuResultVO;
import com.shanzmoo.db.product.mapper.ProductSkuMapper;
import com.shanzmoo.db.product.service.IProductSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品sku 服务实现类
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-09
 */
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSkuEntity> implements IProductSkuService {

    @Override
    public SResponse<PageRespBase<SkuResultVO>> getSkuList(SkuQueryVO skuQueryVO) {
        // 开启分页
        Page page = PageHelper.startPage(skuQueryVO.getPageNum(), skuQueryVO.getPageSize());
        List<SkuResultVO> skuResultVOList = baseMapper.getSkuList(skuQueryVO);
        for (SkuResultVO skuResultVO : skuResultVOList) {
            if(StringUtils.isNotBlank(skuResultVO.getCategoryName1())){
                String categoryNames = skuResultVO.getCategoryName1();
                String[] names = categoryNames.split(",");
                if(names.length != 3) {
                    SResponse.fail(Constant.ERRCODE_PRODUCT_CATEGORYIDLENGTH, "错误商品分类级别:"+names.length);
                }
                skuResultVO.setCategoryName1(names[0]);
                skuResultVO.setCategoryName2(names[1]);
                skuResultVO.setCategoryName3(names[2]);
            }else{
                SResponse.fail(Constant.ERRCODE_PRODUCT_CATEGORYNAME, "商品分类名未找到");
            }
        }
        PageRespBase<SkuResultVO> re = PageRespBase.create(page, skuResultVOList);
        PageHelper.clearPage();
        return SResponse.ok(re);
    }

    @Override
    public List<ProductSkuEntity> getSkuListBySpuId(Integer spuId) {
        List<ProductSkuEntity> productSkuEntityList = baseMapper.selectList(new QueryWrapper<ProductSkuEntity>()
                .eq("deleted", false)
                .eq("parent_id",spuId));
        return productSkuEntityList;
    }

    @Override
    public Integer getSkuIndex(String spu) {
        return baseMapper.getSkuIndex(spu);
    }

    @Override
    public List<SkuResultVO> querySku(List<String> skuCodeList) {
        return baseMapper.querySku(skuCodeList);
    }
}
